import sqlite3
import csv
import pandas as pd


# 连接到SQLite数据库
connection = sqlite3.connect('')
cursor = connection.cursor()

#读取csv列项
id_list = []
with open('.csv', 'r') as file:
    reader = csv.reader(file)
    next(reader)  
    for row in reader:
        id_list.append(row[1])  



# 使用id列表查询数据库
for id in id_list:
    cursor.execute("SELECT * FROM table WHERE id=?", (id,))
    result = cursor.fetchall()
    if result:
        print(f"ID: {id}, 数据: {result}")


# 执行查询语句，获取id项的数据
cursor.execute('')

# 获取所有结果
result = cursor.fetchall()

# 关闭数据库连接
cursor.close()
connection.close()

# 将结果保存到新的CSV文件
with open('id_items.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(['id'])  # 写入列名
    writer.writerows(result)
